package com.tarena.lbs.attach.dao.repository.impl;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.tarena.lbs.attach.dao.mapper.AttachMapper;
import com.tarena.lbs.attach.dao.repository.AttachRepository;
import com.tarena.lbs.attach.pojo.dto.AttachDTO;
import com.tarena.lbs.attach.pojo.param.PicUpdateParam;
import com.tarena.lbs.attach.pojo.po.AttachPO;
import com.tarena.lbs.attach.pojo.query.AttachQuery;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

import java.util.List;


@Slf4j
@Repository
public class AttachRepositoryImpl implements AttachRepository {
    @Autowired
    private AttachMapper attachMapper;

    @Override
    public AttachDTO save(AttachPO attachPO) {
        AttachDTO attachDTO = new AttachDTO();
        attachMapper.insert(attachPO);
        attachDTO.setId(attachPO.getId());
        log.debug("id:{}",attachDTO.getId());
        return attachDTO;
    }

    @Override
    public int batchUpdateAttachByIdOrFileUUID(List<PicUpdateParam> picParams) {

        int count = 0;
        for (PicUpdateParam param : picParams) {
            QueryWrapper<AttachPO> queryWrapper = new QueryWrapper<>();
            log.info("param:{}",param);
            AttachPO attachPO = new AttachPO();
            BeanUtils.copyProperties(param, attachPO);
            log.info("attachPO:{}",attachPO);
            queryWrapper.eq("id", param.getId());
            attachMapper.update(attachPO,queryWrapper);
            count++;
        }
        return count;
    }

    @Override
    public List<AttachDTO> findAttachInfo(AttachQuery attachQuery) {
        QueryWrapper<AttachPO> queryWrapper = new QueryWrapper<>();
        if (attachQuery.getBusinessId() != null) {
            queryWrapper.eq("business_id", attachQuery.getBusinessId());
        }
        if (attachQuery.getBusinessType() != null) {
            queryWrapper.eq("business_type", attachQuery.getBusinessType());
        }
        List<AttachPO> attachPOList = attachMapper.selectList(queryWrapper);
        List<AttachDTO> attachDTOList = attachPOList.stream().map(attachPO -> {
            AttachDTO attachDTO = new AttachDTO();
            BeanUtils.copyProperties(attachPO, attachDTO);
            return attachDTO;
        }).toList();
        return attachDTOList;
    }

    @Override
    public int deleteAttachByBusinessIdAndType(AttachQuery attachQuery) {
       UpdateWrapper<AttachPO> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("business_id", 0);
        updateWrapper.set("business_type", 0);
        updateWrapper.eq("business_id", attachQuery.getBusinessId());
        updateWrapper.eq("business_type", attachQuery.getBusinessType());
        return attachMapper.update(null, updateWrapper);
    }

    @Override
    public int deleteUnBindAttach(AttachQuery attachQuery) {
        QueryWrapper<AttachPO> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("business_id", 0);
        queryWrapper.eq("business_type", 0);
        return attachMapper.delete(queryWrapper);
    }
}
